#!/user/bin/evn Python3 
# -*- coding:utf-8 -*-
# Python.py
# 生成工程目录
# author:zhaohexin

import sys
import os
import configparser

__author__ = 'zhx'
path = os.path.dirname(os.path.abspath(__file__))


def start_project():
    # 从命令行参数取工程名， 默认为de8ug_demo
    project_name = 'zhx_demo'
    if len(sys.argv) > 1:
        project_name = sys.argv[1]

    # 创建目录, readme, __init__文件
    folders = ['bin', 'conf', 'core', 'db', 'log']
    for folder in folders:
        folder_path = os.path.join(path, project_name, folder)
        print(folder_path)
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        # readme
        with open(os.path.join(path, project_name, 'readme.md'), 'w') as f:
            f.write('# ' + project_name + '\n\n')
            f.write('> Author: ' + __author__ + '\n')
        # add init file
        with open(os.path.join(path, project_name, folder, '__init__.py'), 'w'):
            pass
        # add min
        if folder == 'bin':
            with open(os.path.join(path, project_name, folder, 'mainForOracle.py'), 'w') as f:
                f.write('#!/user/bin/evn Python3\n')
                f.write('# -*- coding:utf-8 -*-\n')
                f.write('# Python.py\n')
                f.write('# main\n')
                f.write('# author:zhaohexin\r\n')
                f.write('import sys\n')
                f.write('import os\n')
                f.write('BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n')
                f.write('sys.path.append(BASE_DIR)\n')
    config = configparser.ConfigParser()
    config['DEFAULT'] = {'base_dir': os.path.join(path, project_name)}
    config['DEFAULT']['code_type'] = 'core'
    config['DEFAULT']['db_type'] = 'db'
    config['DEFAULT']['conf_type'] = 'conf'
    config_path = os.path.join(path, project_name, 'conf', 'common.ini')
    with open(config_path, 'w', encoding='utf-8') as f:
        config.write(f)


def main():
    start_project()


if __name__ == '__main__':
    main()
